import requests

if __name__ == '__main__':
    url = "http://127.0.0.1:8080"

    username = "yhzy"
    password = "123"
    # 1.获取验证码
    codeResp = requests.get(url + "/shop/mcheckImg")
    # 获取验证码
    code = codeResp.json()["data"]["code"]
    # 获取cookie信息
    cookie = {"cookie": codeResp.headers["Set-Cookie"]}
    print("1.获取验证码", codeResp.text)

    # 2.用户注册
    # 定义注册用户参数
    registUser = {"username": username, "password": password, "verifyCode": code, "email": "magege@qq.com"}
    # 请求注册
    registUser = requests.post(url + "/shop/mregist", headers=cookie, data=registUser)
    print("2.用户注册", registUser.text)

    # 3.用户登录
    # 定义一个登录的用户
    user = {"username": username, "password": password}
    loginResp = requests.post(url + "/shop/mlogin", data=user, headers=cookie)
    print("3.用户登录", loginResp.text)

    # 4.获取商品分类接口
    categoryResp = requests.get(url + "/shop/mfindAll")
    print("4.获取商品分类接口", categoryResp.text)
    csid = categoryResp.json()["data"][0]["csList"][2]["csid"]

    # 5.通过二级分类获取商品列表
    goodsParam = {"csid":1,"page":1}
    goodsResp = requests.post(url + "/shop/mfindByPageCsid", data=goodsParam)
    print("5.获取二级分类商品列表",goodsResp.text)
    pid = goodsResp.json()["data"]["list"][0]["pid"]

    # 6.根据商品ID查看详情
    goodsInfoResp = requests.post(url+"/shop/mfindByPid",data={"pid":pid})
    print("6.商品详情",goodsInfoResp.text)

    # 7.添加商品到购物车
    cartResp = requests.post(url+"/shop/maddCart",headers=cookie,data={"product.pid":pid,"count":10})
    print("7.添加商品到购物车",cartResp.text)

    # 8.查看购物车
    selectCartResp = requests.post(url+"/shop/mmyCart",headers=cookie)
    print("8.查看购物车",selectCartResp.text)

    # 9.生成订单
    seveOrderResp = requests.get(url+"/shop/msaveOrder",headers=cookie)
    oid = seveOrderResp.json()["data"]["oid"]
    print("9.生成订单",seveOrderResp.text)

    # 10.订单支付
    orderParam = {
             "addr": "北京",
             "name": "zhangsan",
             "oid": oid,
             "pd_FrpId": "BOCO-NET-B2C",
             "phone": "111111111"
                   }
    orderResp = requests.post(url+"/shop/mpayOrder",json=orderParam,headers=cookie)
    print("10.订单支付",orderResp.text)